package top.xana.acg.anime.ui.frag

import android.os.Bundle
import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import top.xana.acg.anime.R
import top.xana.acg.anime.model.Anime
import top.xana.acg.common.base.adp.RecyclerAdapter
import top.xana.acg.common.base.ui.BaseFragment
import top.xana.acg.common.utils.dp2px
import top.xana.acg.common.view.GridSpacingItemDecoration


class SearchListFragment : BaseFragment() {
    private @LayoutRes
    var itemViewType: Int? = null

    companion object {
        const val TAG = "SearchListFragment"

        const val ITEM_VIEW_TYPE = "ITEM_VIEW_TYPE"

        @JvmStatic
        fun newInstance(@LayoutRes viewType: Int) = SearchListFragment().apply {
            arguments = Bundle().apply {
                putInt(ITEM_VIEW_TYPE, viewType)
            }
        }
    }


    private lateinit var viewModel: SearchListViewModel
    override fun getLayoutId() = R.layout.search_list_fragment


    override fun initView() {
        super.initView()
        getView<RecyclerView>(R.id.listView).run {

            when (itemViewType) {
                R.layout.vlayout_hot_view -> {
                    layoutManager = GridLayoutManager(requireContext(), 2)
                    addItemDecoration(GridSpacingItemDecoration(2, dp2px(R.dimen.dp_10), true))
                }
                else -> {
                    layoutManager = LinearLayoutManager(requireContext())
                }
            }

            adapter = object : RecyclerAdapter<Anime>(List(99) { Anime() }
                .toMutableList(), null) {
                override fun getItemViewType(position: Int, data: Anime) =
                    itemViewType!!

                override fun onCreateViewHolder(view: View, viewType: Int): ViewHolder<Anime> {
                    val holder = super.onCreateViewHolder(view, viewType)
                    return holder
                }
            }


        }
    }


    override fun initArg(bundle: Bundle?) {
        super.initArg(bundle)
        arguments.let {
            itemViewType = it?.getInt(ITEM_VIEW_TYPE)

        }
    }


}